@using Fido2NetLib.Objects
@inject ILogger<AuthenticationConfigurationForm> Logger
@inject IScopedPasswordlessClient Passwordless
@inject ICurrentContext CurrentContext

<EditForm Model="FormModel" OnValidSubmit="SubmitAsync" FormName="@FormName" class="space-y-6">
    <DataAnnotationsValidator/>
    <ValidationSummary/>
    <CustomValidationErrors EditContext="FormEditContext" />
    <div class="sm:w-full sm:max-w-md">
        <label class="block text-sm font-medium leading-6 text-gray-900 my-2" for="Purpose">Purpose:</label>
        <InputText id="Purpose" @bind-Value="@FormModel!.Purpose" readonly="@IsPurposeReadOnly" class="text-input"/>

        <label class="block text-sm font-medium leading-6 text-gray-900 my-2" for="UserVerification">User Verification:</label>
        <InputSelect id="UserVerification" @bind-Value="@FormModel.UserVerificationRequirement" TValue="UserVerificationRequirement" class="form-select">
            @foreach (var uv in Enum.GetValues<UserVerificationRequirement>())
            {
                <option value="@uv">@uv</option>
            }
        </InputSelect>

        <label class="block text-sm font-medium leading-6 text-gray-900 my-2" for="Seconds">Time to live (seconds):</label>
        <InputNumber id="Seconds" DisplayName="Time to live (seconds)" @bind-Value="@FormModel.Seconds" min="0" class="text-input"/>

        <label class="block text-sm font-medium leading-6 text-gray-900 my-2" for="Hints">Credential hints:</label>
        <InputText id="Hints" DisplayName="Credential hints" @bind-Value="@FormModel.HintString" class="text-input" />
        <div class="text-sm text-gray-500 mt-1">Put the credential hints in the desired order and combine them in a comma-separated string. Available values: @string.Join(", ", Enum.GetNames<PublicKeyCredentialHint>()).</div>

        <button class="btn-primary mt-3" type="submit">Submit</button>
    </div>
</EditForm>

@code {
    [Parameter]
    public EventCallback<AuthenticationConfigurationFormModel> OnSubmitFunction { get; set; }

    [Parameter]
    public AuthenticationConfigurationFormModel? Model { get; set; }

    [Parameter]
    public bool IsPurposeReadOnly { get; set; }

    [SupplyParameterFromForm]
    public AuthenticationConfigurationFormModel? FormModel { get; set; }
    
    public EditContext? FormEditContext { get; set; }

    public ValidationMessageStore? FormValidationMessageStore { get; set; }

    private const string FormName = "AuthenticationConfigurationForm";

    protected override void OnInitialized()
    {
        if (Model is not null && FormModel is null) FormModel = Model;
        
        FormModel ??= new AuthenticationConfigurationFormModel();
        
        FormEditContext = new EditContext(FormModel);
        FormValidationMessageStore = new ValidationMessageStore(FormEditContext);
    }

    private async Task SubmitAsync()
    {
        try
        {
            await OnSubmitFunction.InvokeAsync(FormModel);
        }
        catch (PasswordlessApiException e)
        {
            FormValidationMessageStore!.Add(() => FormModel!.Purpose, e.Details.Title);
            FormEditContext!.NotifyValidationStateChanged();
            Logger.LogError(e, "Failed to save authentication configuration");
        }
    }
}